<?php
/**
 * util image
 * @package common_lib_util
 * @version [versionNumber] => 1587 [versionString] => ImageMagick 6.3.3 04/21/07 Q16 http://www.imagemagick.org
 */
/**
 * util image
 * @author pandajingjing
 * @todo 提供更多功能
 * @package common_lib_util
 */
class util_image{

	/**
	 * 处理图片的类库
	 * @var emu
	 */
	static $_emuType='imagick';

	/**
	 * 使用Imagick重新绘制图片
	 * @param blob $p_blobImage
	 * @param int $p_intWidth
	 * @param int $p_intHeight
	 * @param string $p_strExtension
	 * @param array $p_arrOption
	 * @return blob
	 */
	static function resizeImage_Imagick($p_blobImage,$p_intWidth,$p_intHeight,$p_strExtension,$p_arrOption=array()){
		$objImage=new Imagick();
		$objImage->readImageBlob($p_blobImage);
		$intOWidth=$objImage->getImageWidth();
		$intOHeight=$objImage->getImageHeight();
		if($intOWidth<$p_intWidth and $intOHeight<$p_intHeight){ //不做拉伸图片处理
			$p_intWidth=$intOWidth;
			$p_intHeight=$intOHeight;
		}
		if(true===$p_arrOption['thumbnail']){
			switch($p_arrOption['mode']){
				case 'cut': //裁剪
					$objImage->cropThumbnailImage($p_intWidth,$p_intHeight);
					break;
				case 'zoom': //缩放
				default:
					switch($p_arrOption['zoom_mode']){
						case 'fill': //填充
							$objImage->thumbnailImage($p_intWidth,$p_intHeight);
							break;
						case 'scale': //等比例缩放
						default:
							switch($p_arrOption['zoom_scale_mode']){
								case 'width':
									$objImage->thumbnailImage($p_intWidth,round($p_intWidth*$intOHeight/$intOWidth),true);
									break;
								case 'height':
									$objImage->thumbnailImage(round($p_intHeight*$intOWidth/$intOHeight),$p_intHeight,true);
									break;
								case 'mix':
								default:
									$objImage->thumbnailImage($p_intWidth,$p_intHeight,true);
									break;
							}
							break;
					}
					break;
			}
		}else{
			switch($p_arrOption['mode']){
				case 'cut':
					$objImage->cropImage($p_intWidth,$p_intHeight,round(($intOWidth-$p_intWidth)/2),round(($intOHeight-$p_intHeight)/2),true);
					break;
				case 'zoom':
				default:
					switch($p_arrOption['zoom_mode']){
						case 'fill':
							$objImage->resizeImage($p_intWidth,$p_intHeight,Imagick::FILTER_CATROM);
							break;
						case 'scale':
						default:
							switch($p_arrOption['zoom_scale_mode']){
								case 'width':
									$objImage->resizeImage($p_intWidth,round($p_intWidth*$intOHeight/$intOWidth),Imagick::FILTER_CATROM,1,true);
									break;
								case 'height':
									$objImage->resizeImage(round($p_intHeight*$intOWidth/$intOHeight),$p_intHeight,Imagick::FILTER_CATROM,1,true);
									break;
								case 'mix':
								default:
									$objImage->resizeImage($p_intWidth,$p_intHeight,Imagick::FILTER_CATROM,1,true);
									break;
							}
							break;
					}
					break;
			}
		
		}
		if(false!==$p_arrOption['watermark']){ //水印
			if(file_exists($p_arrOption['watermark'])){
				load_lib('/util/file');
				$blobWaterMark=util_file::tryReadFile($p_arrOption['watermark']);
				$objWaterMark=new Imagick();
				$objWaterMark->readImageBlob($blobWaterMark);
				$intPosX=$objImage->getImageWidth()-$objWaterMark->getImageWidth()-30;
				$intPosY=$objImage->getImageHeight()-$objWaterMark->getImageHeight()-30;
				$objImage->compositeImage($objWaterMark,Imagick::COMPOSITE_DEFAULT,$intPosX,$intPosY);
				$objWaterMark->clear();
				$objWaterMark->destroy();
			}else{
				trigger_error(__CLASS__.': configuration(resize_watermark) lost.',E_USER_ERROR);
				return false;
			}
		}
		
		$objImage->setImageFormat($p_strExtension);
		$objImage->setImageCompression(Imagick::COMPRESSION_JPEG);
		/* $a = $imagick->getImageCompressionQuality() * 0.75;
	    if ($a == 0) {
	        $a = 75;
	    }
	    $imagick->setImageCompressionQuality($a);*/
		$objImage->stripImage();
		$blobImage=$objImage->getImageBlob();
		$objImage->clear();
		$objImage->destroy();
		return $blobImage;
	}

	/**
	 * 重新绘制图片
	 * @param mix $p_mixParam
	 * @param int $p_intWidth
	 * @param int $p_intHeight
	 * @param string $p_strExtension
	 * @param array $p_arrOption
	 * @return blob
	 */
	static function resizeImage($p_mixParam,$p_intWidth,$p_intHeight,$p_strExtension,$p_arrOption=array()){
		switch(self::$_emuType){
			case 'gd':
				return self::resizeImage_GD($p_mixParam,$p_intWidth,$p_intHeight,$p_strExtension,$p_arrOption);
				break;
			case 'imagick':
				return self::resizeImage_Imagick($p_mixParam,$p_intWidth,$p_intHeight,$p_strExtension,$p_arrOption);
				break;
		}
	}

	/**
	 * 使用GD重新绘制图片
	 * @param string $p_strPath
	 * @param int $p_intWidth
	 * @param int $p_intHeight
	 * @param string $p_strExtension
	 * @param array $p_arrOption
	 * @return blob
	 */
	static function resizeImage_GD($p_strPath,$p_intWidth,$p_intHeight,$p_strExtension,$p_arrOption=array()){
		/* 图片质量
		jpg是否渐进式jpg
		gif是否背景透明
		是否等比例缩放
		是剪裁还是缩放
		缩放的情况下是否要填充空白
		等比例缩放是满足高还是满足宽还是都满足 */
		switch($p_strExtension){
			case 'jpg':
				$objImage=imagecreatefromjpeg($p_strPath);
				break;
		}
		$intOWidth=imagesx($objImage);
		$intOHeight=imagesy($objImage);
		if($intOWidth<$p_intWidth and $intOHeight<$p_intHeight){ //不做拉伸图片处理
			$p_intWidth=$intOWidth;
			$p_intHeight=$intOHeight;
		}
		if(true===$p_arrOption['thumbnail']){
			switch($p_arrOption['mode']){
				case 'cut': //裁剪
					$objImage->cropThumbnailImage($p_intWidth,$p_intHeight);
					break;
				case 'zoom': //缩放
				default:
					switch($p_arrOption['zoom_mode']){
						case 'fill': //填充
							$objImage->thumbnailImage($p_intWidth,$p_intHeight);
							break;
						case 'scale': //等比例缩放
						default:
							switch($p_arrOption['zoom_scale_mode']){
								case 'width':
									$objImage->thumbnailImage($p_intWidth,round($p_intWidth*$intOHeight/$intOWidth),true);
									break;
								case 'height':
									$objImage->thumbnailImage(round($p_intHeight*$intOWidth/$intOHeight),$p_intHeight,true);
									break;
								case 'mix':
								default:
									$objImage->thumbnailImage($p_intWidth,$p_intHeight,true);
									break;
							}
							break;
					}
					break;
			}
		}else{
			switch($p_arrOption['mode']){
				case 'cut':
					$objImage->cropImage($p_intWidth,$p_intHeight,round(($intOWidth-$p_intWidth)/2),round(($intOHeight-$p_intHeight)/2),true);
					break;
				case 'zoom':
				default:
					switch($p_arrOption['zoom_mode']){
						case 'fill':
							$objImage->resizeImage($p_intWidth,$p_intHeight,Imagick::FILTER_CATROM);
							break;
						case 'scale':
						default:
							switch($p_arrOption['zoom_scale_mode']){
								case 'width':
									$objImage->resizeImage($p_intWidth,round($p_intWidth*$intOHeight/$intOWidth),Imagick::FILTER_CATROM,1,true);
									break;
								case 'height':
									$objImage->resizeImage(round($p_intHeight*$intOWidth/$intOHeight),$p_intHeight,Imagick::FILTER_CATROM,1,true);
									break;
								case 'mix':
								default:
									$objImage->resizeImage($p_intWidth,$p_intHeight,Imagick::FILTER_CATROM,1,true);
									break;
							}
							break;
					}
					break;
			}
		
		}
		
		/*if($intOWidth>$p_intWidth or $intOHeight>$p_intHeight){
		 if($p_arrOption){
		$objImage->thumbnailImage($p_intWidth,$p_intHeight,true);
		}else{
		$objImage->resizeImage($p_intWidth,$p_intHeight,Imagick::FILTER_CATROM,1);
		}
		}
		if(false!==$p_arrOption['watermark']){
		if(file_exists($p_arrOption['watermark'])){
		$blobWaterMark=file_get_contents($p_arrOption['watermark']);
		$objWaterMark=new Imagick();
		$objWaterMark->readImageBlob($blobWaterMark);
		$intPosX=$objImage->getImageWidth()-$objWaterMark->getImageWidth()-30;
		$intPosY=$objImage->getImageHeight()-$objWaterMark->getImageHeight()-30;
		$objImage->compositeImage($objWaterMark,Imagick::COMPOSITE_DEFAULT,$intPosX,$intPosY);
		$objWaterMark->clear();
		$objWaterMark->destroy();
		}else{
		trigger_error(__CLASS__.': configuration(resize_watermark) lost.',E_USER_ERROR);
		return false;
		}
		}*/
		
		$objImage->setImageFormat($p_strExtension);
		$objImage->setImageCompression(Imagick::COMPRESSION_JPEG);
		/* $a = $imagick->getImageCompressionQuality() * 0.75;
		 if ($a == 0) {
		$a = 75;
		}
		$imagick->setImageCompressionQuality($a);*/
		$objImage->stripImage();
		$blobImage=$objImage->getImageBlob();
		$objImage->clear();
		$objImage->destroy();
		return $blobImage;
	}

	/**
	 * 生成验证码图片
	 * @param int $p_intWidth
	 * @param int $p_intHeight
	 * @param string $p_strStr
	 * @param int $p_intFontSize
	 * @param int $p_intPointDensity
	 * @param int $p_intCircleDensity
	 * @param int $p_intFontAngle
	 */
	static function createIdentifyCodeImage($p_intWidth,$p_intHeight,$p_strStr,$p_intFontSize=0,$p_intPointDensity=0,$p_intCircleDensity=0,$p_intFontAngle=0){
		//获取各种默认值
		$strTextFont=get_config('imgfont','common');
		if(0==$p_intFontSize){
			$p_intFontSize=round($p_intHeight*3/5);
		}
		if(0==$p_intPointDensity){
			$p_intPointDensity=round($p_intHeight*$p_intWidth/100);
		}
		if(0==$p_intCircleDensity){
			$p_intCircleDensity=round($p_intHeight*$p_intWidth/200);
		}
		//生成画布
		$objImg=imagecreatetruecolor($p_intWidth,$p_intHeight);
		//获取字体范围大小
		$arrTextSize=imagettfbbox($p_intFontSize,$p_intFontAngle,$strTextFont,$p_strStr);
		$intTextHeight=(max($arrTextSize[1],$arrTextSize[3])-min($arrTextSize[5],$arrTextSize[7]));
		$intTextWidth=(max($arrTextSize[4],$arrTextSize[2])-min($arrTextSize[0],$arrTextSize[6]));
		//字体起始位置
		$intTextStartLeft=($p_intWidth-$intTextWidth)/2;
		$intTextStartHeight=$p_intHeight/2+$intTextHeight/2;
		//字体颜色
		$intRed=rand(127,255);
		$intGreen=rand(127,255);
		$intBlue=rand(127,255);
		$objTextColor=imagecolorallocate($objImg,$intRed,$intGreen,$intBlue);
		//往画布上画字符串
		imagettftext($objImg,$p_intFontSize,$p_intFontAngle,$intTextStartLeft,$intTextStartHeight,$objTextColor,$strTextFont,$p_strStr);
		//往画布上画点
		for($i=0;$i<$p_intPointDensity;$i++){
			$intX=rand(0,$p_intWidth);
			$intY=rand(0,$p_intHeight);
			$intRed=rand(0,255);
			$intGreen=rand(0,255);
			$intBlue=rand(0,255);
			$objPointColor=imagecolorallocate($objImg,$intRed,$intGreen,$intBlue);
			imagesetpixel($objImg,$intX,$intY,$objPointColor);
		}
		//往画布上画圆
		/* for($i=0;$i<$circledensity;$i++){
			$x=rand(0,$imgwidth);
			$y=rand(0,$imgheight);
			$r=rand(1,$fontsize/4);
			$red=rand(0,255);
			$green=rand(0,255);
			$blue=rand(0,255);
			$newcolor=imagecolorallocate($objImg,$red,$green,$blue);
			imagefilledellipse($objImg,$x,$y,$r,$r,$newcolor);
		} */
		ob_start();
		imagegif($objImg);
		$blobImage=ob_get_contents();
		ob_end_clean();
		imagedestroy($objImg);
		return $blobImage;
	}
}